<template>
  <Panel>
    <template #title>
      账号添加
    </template>
    <template #content>
      <!-- 表单区域 -->
      <el-form
        :model="userAdd"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item label="用户" prop="account">
          <el-input
            type="text"
            v-model="userAdd.account"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="userAdd.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户组" prop="userGroup">
          <el-select v-model="userAdd.userGroup" placeholder="请选择管理权限">
            <el-option label="超级管理员" value="超级管理员"> </el-option>
            <el-option label="普通管理员" value="普通管理员"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button v-waves @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
  </Panel>
</template>

<script>
import { validateuserName, validatePass } from '@/utils/validation'
import { addUser } from '@/api/user'
export default {
  data() {
    return {
      userAdd: {
        account: '',
        password: '',
        userGroup: '',
        /* 关闭跳转 */
        switch: true
      },
      rules: {
        account: [
          { required: true, validator: validateuserName, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        userGroup: [
          { required: true, message: '请选择管理权限', trigger: 'change' }
        ]
      }
    }
  },

  mounted() {},

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          await addUser(this.userAdd)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-input,
.el-select {
  width: 300px;
}
</style>
